﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using BookiFramework.Files;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BookiFramework.net45.Test
{
    [TestClass]
    public class FileWritterTest
    {
        public const string demoWrite = "DemoWrite.txt";
        public const string demoWriteEmpty = "DemoWriteEmpty.txt";
        public const string demoContent = "Demo";
        public const string demoText = "lorem ipsum";
        
        [TestMethod]
        public void Test_write_text_works_with_empty_file()
        {
            //Arrange
            var file = new FileWritter();

            //Act
            var result = file.WriteText(demoWriteEmpty, demoText, false);

            //Assert
            Assert.IsTrue(result.IsCorrect);
        }

        [TestMethod]
        public void Test_write_text_fail_with_no_exist_file()
        {
            //Arrange
            var file = new FileWritter();

            //Act
            var result = file.WriteText("lalala.txt", demoText, false);

            //Assert
            Assert.IsFalse(result.IsCorrect);
        }

        [TestMethod]
        public void Test_empty_file_works()
        {
            //Arrange
            var file = new FileWritter();

            //Act
            file.WriteText(demoWriteEmpty, demoText, false);
            file.EmptyFile(demoWriteEmpty);
            var result = new FileReader().ReadTextFromFile(demoWriteEmpty);
            
            //Assert
            Assert.AreEqual(result,String.Empty);
        }

        [TestMethod]
        public void Test_write_text__with_new_file_success()
        {
            //Arrange
            var file = new FileWritter();
            var name = String.Format("file{0}.txt", new Random().Next(1,999));
            //Act
            var result = file.WriteTextNewFile(name, demoText,false);
            //Assert
            Assert.IsTrue(result.IsCorrect);
            Assert.IsTrue(File.Exists(name));
        }

        [TestMethod]
        public void Test_write_text__with_new_file_fail_because_exist()
        {
            //Arrange
            var file = new FileWritter();
            var name = String.Format("file{0}.txt", new Random().Next(1, 999));
            File.Create(name);
            //Act
            var result = file.WriteTextNewFile(name, demoText, false);
            //Assert
            Assert.IsFalse(result.IsCorrect);
            Assert.IsTrue(File.Exists(name));
        }

        [TestMethod]
        public void Test_write_text__with_new_file_success_because_exist_but_overwrite()
        {
            //Arrange
            var file = new FileWritter();            
            //Act
            var result = file.WriteTextNewFile(demoText, demoText, true);
            //Assert
            Assert.IsTrue(result.IsCorrect);
            Assert.IsTrue(File.Exists(demoText));
        }
    }
}
